﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hotnet.Entity;
using Hotnet.Dal.BaseRepository;
using Hotnet.Entity.Search;
using System.Linq.Dynamic;

namespace Hotnet.Dal.NSDalCommon
{
	public class DalStar : Repository<Star>
	{
		public Star[] List(int pageNo, int pageSize, out int totalCount, SearchStarCondition ssc)
		{
			string isGroupCondition = string.Format(" IsGroup == {0} ", (ssc.IsGroup ? "true" : "false"));

			string startNameCondition = "";
			if (!string.IsNullOrEmpty(ssc.StarName))
			{
				startNameCondition = string.Format(" && StarName.Contains(\"{0}\") ", ssc.StarName);
			}

			string careerCondition = "";
			if (!string.IsNullOrEmpty(ssc.Career))
			{
				careerCondition = string.Format(" && Career.Contains(\"{0}\")", ssc.Career);
			}

			string zoneCondition = "";
			if (!string.IsNullOrEmpty(ssc.Zone))
			{
				zoneCondition = string.Format(" && Zone.Contains(\"{0}\")", ssc.Zone);
			}

			string sexCondition = "";
			if (!ssc.IsGroup)
			{
				sexCondition = string.Format(" && Sex == {0} ", (ssc.Sex ? "true" : "false"));
			}


			string allCondition = string.Format("{0}{1}{2}{3}{4}", isGroupCondition, startNameCondition, careerCondition, zoneCondition, sexCondition);
			totalCount = disableCacheDataContext.Star.Where(allCondition).Count();
			IQueryable<Star> query = disableCacheDataContext.Star.Where(allCondition).Skip(pageSize * pageNo).Take(pageSize);
	

			return query.ToArray();
		}

		public Star[] ListGroupMembers(int groupId)
		{
			var query = from star in disableCacheDataContext.Star
						where star.GroupID == groupId
						orderby star.StarName
						select star;
			return query.ToArray();
		}

	}
}
